<template>
    <el-dialog :close-on-click-modal="false" ref="dialogform" :title="addEditName + title" :visible.sync="dialogVisible"
        width="50%" :before-close="handleClose">
        <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="120px" class="demo-ruleForm">
            <!-- <el-form-item label="选择项目">
                <el-cascader class="w100p" :options="ownershipList"
                    :props="{ expandTrigger: 'hover', value: 'companyMark', children: 'child', label: 'ownershipName', checkStrictly: true, }"
                    clearable @change="ownership" placeholder="请选择项目" />
            </el-form-item> -->
            <!-- <el-form-item label="物联设备选择" prop="rtuId">
                <el-select class="w100p" v-model="ruleForm.rtuId" placeholder="请选择物联设备" filterable clearable
                    @change="depDataItem">
                    <el-option v-for="item in rtuList" :key="item.rtuId" :label="item.rtuName" :value="item.rtuId" />
                </el-select>
            </el-form-item> -->
            <el-form-item label="物联设备名称" prop="equipmentName">
                <el-input class="w100p" v-model="ruleForm.equipmentName" placeholder="请输入物联设备名称" clearable />
            </el-form-item>
            <el-form-item label="设备编号" prop="equipmentCode">
                <el-input class="w100p" v-model="ruleForm.equipmentCode" clearable placeholder="请输入设备编号"></el-input>
            </el-form-item>
            <el-form-item label="设备类型" prop="equipmentType">
                <el-input class="w100p" v-model="ruleForm.equipmentType" clearable placeholder="请输入设备类型"></el-input>
            </el-form-item>
            <el-form-item label="能源类型" prop="energyId">
                <el-select class="w100p" v-model="ruleForm.energyId" placeholder="请选择能源类型" filterable clearable>
                    <el-option v-for="item in energyList" :key="item.energyId" :label="item.energyName"
                        :value="item.energyId" />
                </el-select>
            </el-form-item>
            <el-form-item label="设备状态" prop="state">
                <el-select class="w100p" v-model="ruleForm.state" placeholder="请选择设备状态" filterable clearable>
                    <el-option v-for="item in stateList" :key="item.value" :label="item.label" :value="item.value" />
                </el-select>
            </el-form-item>
            <el-form-item label="经纬度">
                <div class="w100p">
                    <el-input class="w50p" type="number" v-model="ruleForm.lng" clearable
                        placeholder="请输入经度"></el-input>
                    <el-input class="w50p" type="number" v-model="ruleForm.lat" clearable
                        placeholder="请输入纬度"></el-input>
                </div>
            </el-form-item>
            <el-form-item label="地图" class="w100p">
                <div class="w100p baiduMap">
                    <Map v-if="dialogVisible" :lng="ruleForm.lng" :lat="ruleForm.lat"
                        @getMapCenter="getMapCenter"></Map>
                </div>
            </el-form-item>

        </el-form>
        <span slot="footer" class="dialog-footer">
            <el-button @click="cancel">取 消</el-button>
            <el-button type="primary" @click="submitForm">确 定</el-button>
        </span>
    </el-dialog>
</template>
<script>
import iotApplicationApi from '@/api/iotApplication'
import equipmentApi from '@/api/equipment'
import Map from "@/components/Map"
export default {
    components: { Map },
    props: {
        addEditFlag: {
            type: String,
            default: () => {
                return 'add'
            }
        },
        data: {
            type: Object,
            default: () => {
                return {}
            }
        },
        title: {
            type: String,
            default: () => {
                return '操作表单'
            }
        },
        dialogVisible: {
            type: Boolean,
            default: () => {
                return false
            }
        },

        ownershipList: {
            type: Array,
            default: () => {
                return []
            }
        },
        energyList: {
            type: Array,
            default: () => {
                return []
            }
        },
    },
    data() {
        return {
            addEditName: '添加',
            addEditApi: '',
            ruleForm: {},
            rtuList: [],
            // 0为报废，1为在使用，2为未使用
            stateList: [
                {
                    label: '报废',
                    value: 0
                },
                {
                    label: '在使用',
                    value: 1
                },
                {
                    label: '未使用',
                    value: 2
                }
            ],
            rules: {
                areaName: [
                    { required: true, message: '请输入区域名称', trigger: 'change' }
                ],

                energyId: [
                    { required: true, message: '请选择能源类型', trigger: 'change' }
                ],

                rtuId: [
                    { required: true, message: '请选择设备', trigger: 'change' }
                ],
                equipmentName: [
                    { required: true, message: '请输入设备名称', trigger: 'change' }
                ],
                equipmentCode: [
                    { required: true, message: '请输入设备编号', trigger: 'change' }
                ],
                equipmentType: [
                    { required: true, message: '请输入设备类型', trigger: 'change' }
                ],
            }
        }
    },
    computed: {},
    watch: {
        dialogVisible(v) {
            v && this.initData();
        }
    },

    mounted() { },
    methods: {
        initData() {
            if (this.addEditFlag == 'add') {
                this.resetForm()
                this.addEditApi = equipmentApi.add
                this.addEditName = '添加';
                this.$set(this.ruleForm, 'state', 1);
            } else if (this.addEditFlag == 'edit') {
                this.ruleForm = { ...this.data }
                this.addEditApi = equipmentApi.edit
                this.addEditName = '编辑';
                // this.queryRtuByCompanyMark(sessionStorage.organizationNo)
            }
        },
        getMapCenter(e) {
            this.ruleForm = {
                ...this.ruleForm,
                ...e
            }
        },
        depDataItem(id) {
            let rut = this.rtuList.find(e => e.rtuId == id);
            this.$set(this.ruleForm, 'equipmentName', rut ? rut.rtuName : '')
        },
        // ownership(e) {
        //     if (e) {
        //         let mk = e.slice(-1)
        //         this.queryRtuByCompanyMark(mk)
        //     }
        // },
        // queryRtuByCompanyMark(mk) {
        //     iotApplicationApi.queryRtuByCompanyMark(mk).then(res => {
        //         let data = res.data;
        //         this.rtuList = data;
        //     });
        // },
        //提交表单
        submitForm() {
            this.$refs.ruleForm.validate((valid) => {
                if (valid) {
                    this.addEditApi({ ...this.ruleForm }).then(res => {
                        let msg = res.code == 1 ? '操作成功' : '操作失败'
                        this.$message[res.code == 1 ? 'success' : 'warning'](res.msg || msg)
                        res.code == 1 && this.cancel();
                    }).catch(err => {
                        console.log(err)
                    })
                } else {
                    return false;
                }
            })
        },
        //重置
        resetForm() {
            this.ruleForm = {};
            this.$refs.ruleForm && this.$refs.ruleForm.resetFields()
        },
        //取消关闭
        cancel() {
            this.resetForm()
            this.$emit("closeDialog")
        },
        handleClose(done) {
            this.$confirm('确认关闭？')
                .then(_ => { this.resetForm(); this.cancel() })
                .catch(_ => { });
        }
    }
};
</script>
<style lang="scss" scoped>
.baiduMap {
    height: 300px;
}
</style>